package com.heisen.system.controller;


import com.heisen.base.entity.Api;
import com.heisen.base.exception.RRException;
import com.heisen.base.utils.MyObjectUtils;
import com.heisen.database.controller.AbstractController;
import com.heisen.security.annotation.ApiResourceOpen;
import com.heisen.system.dto.RoleDto;
import com.heisen.system.dto.UpdateUserPasswordDto;
import com.heisen.system.dto.UserDto;
import com.heisen.system.entity.Role;
import com.heisen.system.entity.User;
import com.heisen.system.query.RoleQuery;
import com.heisen.system.query.UserQuery;
import com.heisen.system.service.IRoleService;
import com.heisen.system.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.validation.Valid;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author sen
 * @since 2023-12-24
 */
@RestController
@RequestMapping("/user")
public class UserController extends AbstractController<User, UserDto, UserQuery, IUserService> {

    @Autowired
    private IUserService userService;

    /**
     * 获取登入用户
     * @param username
     * @return
     */
    @ApiResourceOpen
    @RequestMapping("findUserByUsername")
    public Api LoginByName(@RequestParam("username") String username){
        return Api.ok(userService.findByName(username));
    }

    /**
     * 获取用户权限
     * @param username
     * @return
     */
    @RequestMapping("findUserPermissions")
    public Api findUserPermissions(@RequestParam("username") String username){
        return Api.ok(userService.findUserPermissions(username));
    }



    /**
     * 用户 修改密码
     * @param updateUserPasswordDto
     * @return
     */
    @PostMapping("/updateSysUserPass.json")
    public Api updateSysUserPass( @Valid @RequestBody UpdateUserPasswordDto updateUserPasswordDto){
        if(MyObjectUtils.isNotEquals(updateUserPasswordDto.getPassword(),updateUserPasswordDto.getPasswordAgain())) throw new RRException("密码不一致");

        return Api.of( service.updateSysUserPass(updateUserPasswordDto))
                .failMsg("用户修改密码失败")
                .build();
    }

}
